package com.xiaot.shopping.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaot.common.utils.PageUtils;
import com.xiaot.common.utils.R;
import com.xiaot.shopping.product.entity.AttrGroupEntity;
import com.xiaot.shopping.product.service.AttrGroupService;
import com.xiaot.shopping.product.service.AttrService;
import com.xiaot.shopping.product.vo.AttrGroupRelationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 属性分组
 *
 * @author xiaoT
 * @email 
 * @date 2021-07-02 22:36:20
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private AttrService attrService;

    @RequestMapping("/{attrGroupId}/attr/relation")
    public R relation(@PathVariable Integer attrGroupId) {
        return R.ok().put("data", attrService.getRelationAttr(attrGroupId));
    }

    @RequestMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] attrGroupRelation) {
        attrGroupService.deleteRelation(attrGroupRelation);
        return R.ok();
    }

    @RequestMapping("/{attrGroupId}/noattr/relation")
    public R noRelation(@PathVariable Integer attrGroupId, @RequestParam Map<String, Object> params) {
        return R.ok().put("page", attrService.getNoRelationAttr(params, attrGroupId));
    }

    @RequestMapping("/attr/relation")
    public R saveAttrGroupRelation(@RequestBody AttrGroupRelationVo[] attrGroupRelationVos) {
        attrService.saveAttrGroupRelation(attrGroupRelationVos);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catId}")
//    @RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable Long catId){
        PageUtils page = attrGroupService.queryPage(params, catId);

        return R.ok().put("page", page);
    }

    /**
     * 列表(不分页)
     */
    @RequestMapping("/listAll/{catId}")
//    @RequiresPermissions("product:attrgroup:list")
    public R listAll(@PathVariable Long catId){
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        if (catId != 0) {
            queryWrapper.eq("catelog_id", catId);
        }
        List<AttrGroupEntity> list = attrGroupService.list(queryWrapper);

        return R.ok().put("list", list);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
//    @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        attrGroup.setCatelogPath(attrGroupService.findCatelogPath(attrGroup.getCatelogId()));
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
